﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.Net.NetworkInformation;

namespace ClockSync2
{
    class tcpServer
    {
        public Socket socket;
        public TcpListener tcpListener;
        IPAddress ipAddress;
        public Neighbour ident;
        TcpClient cc;

        public tcpServer(IPAddress ipAddress, int port)
        {

            try
            {

                // IPAddress ipAddress = IPAddress.Parse(ip); //use local m/c IP address, and use the same in the client
                /* Initializes the Listener */
                this.ipAddress = ipAddress;
                tcpListener = new TcpListener(ipAddress, port);
                /* Start Listeneting at the specified port */
                tcpListener.Start();
                //Console.WriteLine("The server is running at port 8001...");
                //Console.WriteLine("The local End point is :" + tcpListener.LocalEndpoint);
                //Console.WriteLine("Waiting for a connection.....");
            }
            catch (Exception e)
            {
                Console.WriteLine("Error in TCPServer on port: "+ port + "\n" + e.StackTrace);
            }
        }

        public tcpServer(IPAddress ipAddress, int port,Neighbour n)
        {
            this.ident = n;
            try
            {

                // IPAddress ipAddress = IPAddress.Parse(ip); //use local m/c IP address, and use the same in the client
                /* Initializes the Listener */
                this.ipAddress = ipAddress;
                tcpListener = new TcpListener(ipAddress, port);
                /* Start Listeneting at the specified port */
                tcpListener.Start();
                //Console.WriteLine("The server is running at port 8001...");
                //Console.WriteLine("The local End point is :" + tcpListener.LocalEndpoint);
                //Console.WriteLine("Waiting for a connection.....");    
                
            }
            catch (Exception e)
            {
                Console.WriteLine("Error in TCPServer on port: " + port + "\n" + e.StackTrace);
            }
        }


        public byte[] listenAndGetMsg()
        {           
            byte[] msg=null;
            System.Threading.Thread newThread;
            newThread = new System.Threading.Thread(() => { msg = getMsg(); });
            newThread.IsBackground = true;
            newThread.Start();
            newThread.Join();

            return msg;
        }
                
        public void exit()
        {
            tcpListener.Stop();
            socket.Close();
        }

        public byte[] getMsg()
        {
            Console.WriteLine("in tcpServer getting msg from " + ipAddress + " ");
            
            byte[] word = new byte[100];
            if(cc==null)
                cc = tcpListener.AcceptTcpClient();

            NetworkStream stream = cc.GetStream();    
            int k = stream.Read(word,0,word.Length);
                
            
            byte[] ret = new byte[k];
                        
            for (int i = 0; i < k; i++)
                ret[i]= word[i];

            Console.WriteLine("in tcpServer got "+utils.ByteToStrArray(word));
            return word;
        }
              
    }
}